<?php
include dirname(__FILE__) . "/config/functions.php";

$optionsQuery = mysql_query("SELECT * FROM `option`");
while($row = mysql_fetch_assoc($optionsQuery))    $options[$row['key']] = $row['value'];

//get urls
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    $urls = getUrls(isset($_GET['refName']) ? $_GET['refName'] : '');
    $_SESSION['urls'] = $urls;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $urls = $_SESSION['urls'];
    //validate form
    $error = '';

    foreach ($urls as $key => $value) {
        if ($_POST['code' . $key] == '') {
            $error .= ($key) ? 'Invalid Ad #' . $key . ' code.<br>' : 'Invalid Sponsor Ad code.<br>';
        } elseif ($_POST['code' . $key] != $_SESSION['code_' . $key]) {
            $error .= ($key) ? 'Invalid Ad #' . $key . ' code.<br>' : 'Invalid Sponsor Ad code.<br>';
        }
    }

    if ($_POST['name'] == '') {
        $error .= 'Full Name Field can\'t be blank.<br>';
    } elseif (strlen($_POST['name']) > 50) {
        $error .= 'Full Name Field can\'t exceed 50 charachters.<br>';
    }
    if ($_POST['email'] == '') {
        $error .= 'Email Field can\'t be blank.<br>';
    } elseif (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
        $error .= 'Invalid Email address.<br>';
    }
    if ($_POST['username'] == '') {
        $error .= 'Username Field can\'t be blank.<br>';
    } elseif (strlen($_POST['username']) > 50) {
        $error .= 'Username Field can\'t exceed 50 charachters.<br>';
    }
    if ($_POST['password'] == '') {
        $error .= 'Password Field can\'t be blank.<br>';
    } elseif (strlen($_POST['password']) > 50) {
        $error .= 'Password Field can\'t exceed 50 charachters.<br>';
    }
    if ($_POST['cpassword'] == '') {
        $error .= 'Confirm Password Field can\'t be blank.<br>';
    } elseif (strlen($_POST['cpassword']) > 50) {
        $error .= 'Confirm Password Field can\'t exceed 50 charachters.<br>';
    } elseif ($_POST['password'] != $_POST['cpassword']) {
        $error .= 'Password Doesn\'t match confirm Password.<br>';
    }
    if ($_POST['textAd'] == '') {
        $error .= 'Text Ad Field can\'t be blank.<br>';
    } elseif (strlen($_POST['textAd']) > 250) {
        $error .= 'Text Ad Field can\'t exceed 250 charachters.<br>';
    }
    if ($_POST['url'] == '') {
        $error .= 'Url Field can\'t be blank.<br>';
    } elseif (strlen($_POST['url']) > 250) {
        $error .= 'Url Field can\'t exceed 250 charachters.<br>';
    }
    if (!isset($_POST['terms'])) {
        $error .= 'You can only join if you agree to terms and conditions.<br>';
    }

    //if all valid then validate username
    if (!$error) {

        $username = mysql_real_escape_string($_POST['username']);
        $rowsu = mysql_query("SELECT username from user WHERE username = '$username'");
        if (mysql_num_rows($rowsu) > 0) {
            $error .= 'Username already exists, please enter another one.<br>';
        }
    }

    //if all valid then save and send mail.
    if (!$error) {

        saveNewAccount($_POST);
    }
}
?>
<!doctype html>
<!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en"> <![endif]-->
<!--[if IE 7]>    <html class="no-js lt-ie9 lt-ie8" lang="en"> <![endif]-->
<!--[if IE 8]>    <html class="no-js lt-ie9" lang="en"> <![endif]-->
<!-- Consider adding a manifest.appcache: h5bp.com/d/Offline -->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en"> <!--<![endif]-->
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <title><?= $options['site_name'] ?></title>
        <meta property="og:title" content="<?= $options['site_name'] ?>">
        <meta property="og:site_name" content="<?= $options['site_name'] ?>">
        <meta property="og:type" content="<?= $options['type'] ?>">
        <meta property="og:url" content="<?= $options['url'] ?>">
        <meta property="og:image" content="<?= $options['image'] ?>">
        <meta property="fb:admins" content="<?= $options['admins'] ?>">
        <meta name="description" content="<?= $options['site_description'] ?>">
        <meta name="keywords" content="<?= $options['keywords'] ?>">
        <meta name="author" content="<?= $options['author'] ?>">
        <meta name="Robots" content="<?= $options['Robots'] ?>">
        <meta http-equiv="Content-Type" content="text/html; charset=windows-1250">
        <link href="css/bootstrap.min.css" rel="stylesheet"/>
        <link href="css/style.css" rel="stylesheet"/>
    </head>
    <body>
        <!-- Save for Web Slices (index.psd) -->
        <table width="950" height="1" border="0" align="center" cellpadding="0" cellspacing="0" id="Table_01">
            <tr>
                <td background="images/bg.png">
                    <img src="images/header.png" alt="Viral Marketing Script - Your MEGA Traffic on Over-Drive!" width="950" height="294" lowsrc="Viral Marketing Script - Your MEGA Traffic on Over-Drive!"></td>
            </tr>
            <tr>
                <td background="images/bg.png"><div align="center"><strong><span class="style4">
                                :: <a href="index.php">Home</a> 
                                :: <a href="contact.php">Contact Us</a> 
                                :: <a href="advertise.php">Advertise</a> 
                                :: <a href="member/login.php">Login</a> 
                                ::</span></strong></div>
                </td>
            </tr>
            <tr>
                <td background="images/bg.png">
                    <div align="center">
                        <div class="container"> 
                            <div style="margin: 7px;"> 
                                <?php $_position = 'top'; ?>
                                <?php include dirname(__FILE__) . '/_banner.php'; ?>
                            </div>
                            <div class="row">

                                <?php if (isset($error)): ?>
                                    <div class="alert alert-danger alert-dismissable">
                                        <?= $error; ?>
                                    </div>
                                <?php endif; ?> 

                                <div class="main text-center text-page"> 
                                    <?php
                                    $content = mysql_query("SELECT `value` FROM `option` where `key` = 'home_page' ");
                                    $content = mysql_fetch_assoc($content);
                                    $levelNumber = mysql_query("SELECT `value` FROM `option` where `key` = 'number_of_levels' ");
                                    $levelNumber = mysql_fetch_assoc($levelNumber);
                                    $content = str_replace("{no_of_levels}", $levelNumber['value'] + 1, $content);
                                    echo $content['value'];
                                    ?>
                                    <table class="table table-bordered homeTable">

                                        <?php foreach ($urls as $key => $value): ?> 

                                            <tr>
                                                <td><?= ($key == 0) ? 'Sponsor Ad' : $key; ?></td>
                                                <td><a href="ads.php?id=<?= $key ?>" target="_blank"><?= $value['textAd'] ?></a></td>
                                            </tr>
                                            <?php
                                            if (!isset($_SESSION['code_' . $key])) {
                                                $_SESSION['code_' . $key] = generateRandomString(6);
                                            }
                                            //save in session
                                            $_SESSION['url_' . $key] = $value['url'];
                                            $_SESSION['url_id_' . $key] = $value['id'];
                                            ?>
                                        <?php endforeach; ?>
                                    </table>
                                    <img src="images/lineDivider.png" >


                                    <div style="color: black;" class="signupFormDiv">
                                        <p>Enter the Ad Codes Below</p>
                                        <form action="" method="post" class="form-horizontal signupForm" role="form">
                                            <div class="form-group">
                                                <?php foreach ($urls as $key => $value): ?> 
                                                    <div class="col-xs-2 custom">
                                                        <label><?= ($key) ? addOrdinalNumberSuffix($key) : 'Sponsor'; ?> Ad</label>
                                                        <input required='required'type="text" class="form-control" name="code<?= $key ?>" size="6" value="<?= (isset($_POST['code' . $key])) ? $_POST['code' . $key] : ''; ?>">
                                                    </div>
                                                <?php endforeach; ?>
                                            </div>

                                            <div class="form-group">
                                                <label for="fullName" class="col-sm-2 control-label">Full Name</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="text" class="form-control" name="name" size="15" value="<?= (isset($_POST['name'])) ? $_POST['name'] : ''; ?>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="emailAddress" class="col-sm-2 control-label">Email Address</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="text" class="form-control" name="email" size="15"  value="<?= (isset($_POST['email'])) ? $_POST['email'] : ''; ?>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="userName" class="col-sm-2 control-label">User Name</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="text" class="form-control" name="username" size="15"  value="<?= (isset($_POST['username'])) ? $_POST['username'] : ''; ?>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="password" class="col-sm-2 control-label">Password</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="password" class="form-control" name="password" size="15">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="confirmPassword" class="col-sm-2 control-label">Confirm Password</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="password" class="form-control" name="cpassword" size="15">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="textAd" class="col-sm-2 control-label">Text Ad</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="text" class="form-control" name="textAd" size="25" maxlength="100"  value="<?= (isset($_POST['textAd'])) ? $_POST['textAd'] : ''; ?>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <label for="yourUrl" class="col-sm-2 control-label">Your URL</label>
                                                <div class="col-sm-6">
                                                    <input required='required'type="text" class="form-control" name="url" size="25" value="<?= (isset($_POST['url'])) ? $_POST['url'] : 'http://'; ?>">
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-sm-offset-2 col-sm-6">
                                                    <div class="checkbox">
                                                        <label class="agreeLabel">
                                                            <input style="margin-right: 10px;float: none" required='required'type="checkbox" name="terms" value="1">I have read and agree to the <a href="terms.php" target="_blank">Terms of Service</a>
                                                    </div>
                                                </div>
                                            </div>
                                            <div class="form-group">
                                                <div class="col-sm-offset-2 col-sm-6">
                                                    <button type="submit" class="btn btn-default">Join Viral Marketing</button>
                                                </div>
                                            </div>
                                            <input type="hidden" name="refName" value="<?= (isset($_GET['refName']) && strlen($_GET['refName']) > 0) ? $_GET['refName'] : null ?>" />
                                        </form>
                                        <div>


                                        </div> 

                                        <div style="margin: 25px;"> 
                                            <?php $_position = 'bottom'; ?>
                                            <?php include dirname(__FILE__) . '/_banner.php'; ?>
                                        </div>

                                    </div> 
                                </div>

                            </div>
                            </td>
                            </tr>
                            <tr>
                                <td background="images/bg.png"><div align="center">
                                        <?php include 'footer.php'; ?>
                                    </div></td>
                            </tr>
                            <tr>
                                <td>
                                    <img src="images/footer.png" width="950" height="108" alt=""></td>
                            </tr>
                            </table>
                            <script src="js/jquery-1.10.2.min.js"></script>
                            <script src="js/bootstrap.min.js"></script>

                            <!-- End Save for Web Slices -->
                            </body>
                            </html>    